package views;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.DbHelper;
import presenters.CreateAccountPresenter;


public class CreateNewAccountPanel extends JPanel {
	private CreateAccountPresenter myPresenter;
	private JTextField textField; // Username
	private JTextField textField_1; // Password
	private JTextField textField_2; // Confirm password
	JComboBox comboBox;
	

	/**
	 * Create the panel.
	 */
	public CreateNewAccountPanel() {
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		
		JPanel panel = new JPanel();
		add(panel);
		panel.setLayout(new BorderLayout(0, 0));
		DbHelper myHelper = new DbHelper();
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2, BorderLayout.CENTER);
		
		Box verticalBox = Box.createVerticalBox();
		panel_2.add(verticalBox);
		
		Component verticalStrut_7 = Box.createVerticalStrut(60);
		verticalBox.add(verticalStrut_7);
		
		JLabel lblUsername = new JLabel("Username:");
		verticalBox.add(lblUsername);
		
		Component verticalStrut_4 = Box.createVerticalStrut(20);
		verticalBox.add(verticalStrut_4);
		
		textField = new JTextField();
		verticalBox.add(textField);
		textField.setColumns(10);
		
		Component verticalStrut_8 = Box.createVerticalStrut(20);
		verticalBox.add(verticalStrut_8);
		
		JLabel lblPassword = new JLabel("Password:");
		verticalBox.add(lblPassword);
		
		Component verticalStrut_3 = Box.createVerticalStrut(20);
		verticalBox.add(verticalStrut_3);
		
		textField_1 = new JTextField();
		verticalBox.add(textField_1);
		textField_1.setColumns(10);
		
		Component verticalStrut_9 = Box.createVerticalStrut(20);
		verticalBox.add(verticalStrut_9);
		
		JLabel lblConfirmPassword = new JLabel("Confirm Password:");
		verticalBox.add(lblConfirmPassword);
		
		Component verticalStrut_2 = Box.createVerticalStrut(20);
		verticalBox.add(verticalStrut_2);
		
		textField_2 = new JTextField();
		verticalBox.add(textField_2);
		textField_2.setColumns(10);
		
		Component verticalStrut_10 = Box.createVerticalStrut(20);
		verticalBox.add(verticalStrut_10);
		
		JLabel lblTypeOfUser = new JLabel("Type of User:");
		verticalBox.add(lblTypeOfUser);
		
		Component verticalStrut_5 = Box.createVerticalStrut(20);
		verticalBox.add(verticalStrut_5);
		
		comboBox = new JComboBox(myHelper.getUserTypes());
		verticalBox.add(comboBox);
		
		Component verticalGlue = Box.createVerticalGlue();
		verticalGlue.setEnabled(false);
		verticalBox.add(verticalGlue);
		
		Component verticalStrut_1 = Box.createVerticalStrut(60);
		verticalBox.add(verticalStrut_1);
		
		JButton btnCreateAccount = new JButton("Create  Account");
		verticalBox.add(btnCreateAccount);
		btnCreateAccount.addActionListener(new MyActionListener());
		
		Component verticalStrut = Box.createVerticalStrut(20);
		panel.add(verticalStrut, BorderLayout.SOUTH);
		
		JLabel lblNewLabel = new JLabel("Create An Account");
		panel.add(lblNewLabel, BorderLayout.NORTH);

		myPresenter = new CreateAccountPresenter(this);
	}
	
	private class MyActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String username = textField.getText();
			String password = textField_1.getText();
			String confPassword = textField_2.getText();
			String choice = String.valueOf(comboBox.getSelectedItem());
			myPresenter.createAccount(username, password, confPassword, choice);
		}
		
	}

}
